﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject.UserAccount;
using Jalal.Library.DataStructure;
using System.Collections.Generic;
using Jalal.Library.Utility;
using System.Drawing;
using BCGC.BLL.BusinessObject.Announcement;

public partial class Admin_Announcement_Default : AdminBasePage
{
    private int CurrentPageIndex
    {
        get
        {
            if (ViewState["CurrentPageIndex"] == null)
                ViewState["CurrentPageIndex"] = 1;
            return (int)ViewState["CurrentPageIndex"];
        }
        set { ViewState["CurrentPageIndex"] = value; }
    }

    private string SortOrder
    {
        get
        {
            if (ViewState["SortOrder"] == null)
                ViewState["SortOrder"] = "Id";
            return (string)ViewState["SortOrder"];
        }
        set { ViewState["SortOrder"] = value; }
    }

    private int TotalData
    {
        get
        {
            if (ViewState["TotalData"] == null)
                ViewState["TotalData"] = 0;
            return (int)ViewState["TotalData"];
        }
        set { ViewState["TotalData"] = value; }
    }

    private void BindPager()
    {
        List<string> pages = new List<string>();
        int pageCount = (TotalData / grvAnnouncements.PageSize) + ((TotalData % grvAnnouncements.PageSize) > 0 ? 1 : 0);
        for (int i = 1; i <= pageCount; i++)
            pages.Add(i.ToString());

        rptPager.DataSource = pages;
        rptPager.DataBind();
    }

    protected void Page_Change(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "PagingCommand")
        {
            CurrentPageIndex = Convert.ToInt32(e.CommandArgument);
            BindGrid();
            BindPager();
        }
    }

    protected void Grid_Sort(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Sort")
        {
            grvAnnouncements.EditIndex = -1;
            SortOrder = (string)e.CommandArgument;
            BindGrid();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
            BindPager();
        }
    }

    private void BindGrid()
    {
        bool? isActive;
        if (ddlActive.SelectedValue == "-1")
            isActive = null;
        else
            isActive = Security.BindBooleanInput(ddlActive.SelectedValue, SecurityLevel.Three);

        bool? isFeatured;
        if (ddlFeatured.SelectedValue == "-1")
            isFeatured = null;
        else
            isFeatured = Security.BindBooleanInput(ddlFeatured.SelectedValue, SecurityLevel.Three);

        int totalData = 0;
        ReadWriteCustomCollection<AnnouncementItem> announcements = AnnouncementItem.GetAllAnnouncements(CurrentPageIndex, SortOrder, grvAnnouncements.PageSize,
            string.IsNullOrEmpty(tbxTitle.Text) ? null : Security.BindStringInput(tbxTitle.Text, SecurityLevel.Three),
            createDateFrom.Date, createDateTo.Date, isActive, isFeatured, out totalData);

        TotalData = totalData;

        grvAnnouncements.DataSource = announcements;
        grvAnnouncements.DataBind();
    }

    protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox cbxSelectAll = (CheckBox)e.Row.FindControl("cbxSelectAll");
            cbxSelectAll.Attributes.Add("onclick", "javascript:SelectAll('" + grvAnnouncements.ClientID
                + "','" + cbxSelectAll.ClientID + "','cbxSelect')");
        }
        else if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal ||
            e.Row.RowState == DataControlRowState.Alternate))
        {
            AnnouncementItem announcement = (AnnouncementItem)e.Row.DataItem;

            HyperLink hlkEdit = (HyperLink)e.Row.FindControl("hlkEdit");
            if (hlkEdit != null && announcement != null)
            {
                hlkEdit.NavigateUrl = CustomSiteMap.EditAnnouncement + "?id=" + announcement.Id.ToString();
            }
        }
    }

    protected void Pager_DataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (Convert.ToInt32(e.Item.DataItem) == CurrentPageIndex)
            {
                LinkButton lkbPageIndex = (LinkButton)e.Item.FindControl("lkbPageIndex");
                lkbPageIndex.CssClass = "page-index-selected";
            }
        }
    }

    protected void Delete_Click(object sender, EventArgs e)
    {
        List<int> announcementsToDelete = new List<int>();

        foreach (GridViewRow row in grvAnnouncements.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cbxSelect = (CheckBox)row.FindControl("cbxSelect");
                if (cbxSelect != null && cbxSelect.Checked)
                {
                    Label lblId = (Label)row.FindControl("lblId");
                    announcementsToDelete.Add(Convert.ToInt32(lblId.Text));
                }
            }
        }
        AnnouncementItem.Delete(announcementsToDelete);
        BindGrid();
        BindPager();
    }

    protected void UpdateFeatured_Click(object sender, EventArgs e)
    {
        List<int> featuredAnnouncements = new List<int>();
        List<int> nonFeaturedAnnouncements = new List<int>();

        foreach (GridViewRow row in grvAnnouncements.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cbxSelect = (CheckBox)row.FindControl("cbxFeatured");
                if (cbxSelect != null)
                {
                    Label lblId = (Label)row.FindControl("lblId");
                    if (cbxSelect.Checked)
                    {
                        featuredAnnouncements.Add(Convert.ToInt32(lblId.Text));
                    }
                    else
                    {
                        nonFeaturedAnnouncements.Add(Convert.ToInt32(lblId.Text));
                    }
                }
            }
        }
        AnnouncementItem.UpdateFeatured(featuredAnnouncements, nonFeaturedAnnouncements);
        BindGrid();
    }

    protected void UpdateActive_Click(object sender, EventArgs e)
    {
        List<int> activeAnnouncements = new List<int>();
        List<int> inactiveAnnouncements = new List<int>();

        foreach (GridViewRow row in grvAnnouncements.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cbxSelect = (CheckBox)row.FindControl("cbxActive");
                if (cbxSelect != null)
                {
                    Label lblId = (Label)row.FindControl("lblId");
                    if (cbxSelect.Checked)
                    {
                        activeAnnouncements.Add(Convert.ToInt32(lblId.Text));
                    }
                    else
                    {
                        inactiveAnnouncements.Add(Convert.ToInt32(lblId.Text));
                    }
                }
            }
        }
        AnnouncementItem.UpdateActive(activeAnnouncements, inactiveAnnouncements);
        BindGrid();
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        BindGrid();
        BindPager();
    }
}
